//
//  TaskTableViewCell.m
//  leo
//
//  Created by 常艳 on 15/9/19.
//  Copyright (c) 2015年 mdx. All rights reserved.
//

#import "TaskTableViewCell.h"
#import "Tooler.h"
#import "AppDelegate.h"
#import "RecordTaskTableViewCell.h"
@implementation TaskTableViewCell
@synthesize task;
- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}
-(void)initUI
{
//    self.contentView.layer.borderColor =  [UIColor colorWithRed:0.79 green:0.88 blue:0.92 alpha:1].CGColor;
//    self.contentView.layer.borderWidth = 1.0;
    if(task==NULL)
        return;
    if(task.title!=NULL&&![task.title isEqualToString:@"(null)"])
        self.titleLable.text=task.title;
    if(task.desc!=NULL&&![task.desc isEqualToString:@"(null)"])
    {
        self.descLable.text=task.desc;
    }
    self.empLable.text=[NSString stringWithFormat:@"完成后获得%d点经验值",task.empirical];
    if(task.status==DONE)
    {
//        self.statusLable.text=@"完成";
        self.statusImg.image=[UIImage imageNamed:@"task_already@2x.png"];
        AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
        if(mydelegate!=NULL&&mydelegate.taskCell!=NULL)
        {
            if(mydelegate.taskCell==self)
            {
                self.playBtn.imageView.animationImages=[NSArray arrayWithObjects:
                                                        [UIImage imageNamed:@"voice_g_anim1.png"],
                                                        [UIImage imageNamed:@"voice_g_anim2.png"],
                                                        [UIImage imageNamed:@"voice_g_anim3.png"],                                           nil];
                [self.playBtn.imageView setAnimationDuration:2.0f];
                [self.playBtn.imageView startAnimating];
            }
        }
    }
    else if(task.status==UNDO)
    {
        self.statusImg.hidden = NO;
       
        self.statusImg.image=[UIImage imageNamed:@"task_unalready@2x.png"];
    }
    else if(task.status==EXPIRE||[Tooler istaskExpire:task])
    {   self.statusImg.hidden = NO;
        self.statusImg.image=[UIImage imageNamed:@"task_timeOut@2x.png"];
        
    }
    else if(task.status==DOING)
    {
//        self.statusLable.text=@"进行中";
//        self.statusImg.image=[UIImage imageNamed:@"grow_label_doing@2x.png"];
        self.statusImg.hidden = YES;
    }


    if(task.audioUrl!=NULL&&![task.audioUrl isEqualToString:@"(null)"]&&![task.audioUrl isEqualToString:@""])
    {
        self.recordImg.hidden=NO;
        self.playBtn.hidden=NO;
        self.timeLable.hidden=NO;
        self.timeLable.text=[NSString stringWithFormat:@"%d",task.duration];
    }
    else
    {
        self.recordImg.hidden=YES;
        self.playBtn.hidden=YES;
        self.timeLable.hidden=YES;
    }
    
}
- (IBAction)playRecoed:(id)sender {
    [self playAction];
}
-(void)playAction
{
    AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
    if(mydelegate!=NULL)
    {
        if(mydelegate.recordTaskCell!=NULL)
        {
            [mydelegate.recordTaskCell stop];
            mydelegate.recordTaskCell=NULL;
        }
        if(mydelegate.taskCell!=NULL&&mydelegate.taskCell!=self)
        {
            [mydelegate.taskCell stop];
            mydelegate.taskCell=NULL;
        }
        mydelegate.taskCell=self;
    }
    if(!self.isPlay)
    {
        self.playBtn.imageView.animationImages=[NSArray arrayWithObjects:
                                                [UIImage imageNamed:@"voice_g_anim1.png"],
                                                [UIImage imageNamed:@"voice_g_anim2.png"],
                                                [UIImage imageNamed:@"voice_g_anim3.png"],                                           nil];
        [self.playBtn.imageView setAnimationDuration:2.0f];
        [self.playBtn.imageView startAnimating];
        [self play:task.audioUrl];
        self.isPlay=YES;
    }
    else
    {
        [self.player stop];
        [self.playBtn.imageView stopAnimating];
        self.playBtn.imageView.animationImages=NULL;
        self.isPlay=NO;
    }
    
}
-(void)play:(NSString *)path
{
    if(path==NULL)
    {
        NSLog(@"%@,play,path is null.",promptTask_tag);
        return;
    }
    path=[[Tooler getDocumentDirectory] stringByAppendingString:path];
    NSLog(@"%@,play,path is %@.",promptTask_tag,path);
    //文件管理器
    NSFileManager *fileManager = [NSFileManager defaultManager];
    //判断数据库是否存在
    BOOL find = [fileManager fileExistsAtPath:path];
    if(find)
    {
        AVAudioSession *audioSession=[AVAudioSession sharedInstance];
        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
        [audioSession setActive:YES error:nil];
        NSError *error=nil;
        self.player=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL URLWithString:path] error:&error];
        if(error!=nil)
        {
            NSLog(@"%@,playRecord,create player error,error is %@.",promptTask_tag,error);
            return;
        }
        self.player.delegate=self;
        self.player.numberOfLoops=0;
        [self.player setVolume:1.0];
        [self.player prepareToPlay];
        [self.player play];
    }
    else
    {
        NSLog(@"%@,playRecord,the record file is not exist.",promptTask_tag);
    }
}
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
    [self stop];
    AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
    if(mydelegate!=NULL&&mydelegate.taskCell!=NULL)
    {
        if(mydelegate.taskCell==self)
        {
            mydelegate.taskCell=NULL;
        }
    }
}
-(void)stop
{
    if(self.player!=NULL)
    {
        [self.player stop];
        [self.playBtn.imageView stopAnimating];
        self.playBtn.imageView.animationImages=NULL;
    }
    self.isPlay=NO;
}

@end
